package event.handlers;

import java.util.List;
import serializare.SerializationToXml;
import bean.Person;
import core.controller.EventHandler;

public class AddEventHandler extends EventHandler {

    @Override
    public String perform() {
        String nextUrl = getNextURL("HOME");
        List<Person> persons = SerializationToXml.deserializeXMLToObject();
        try {
            Person pers = new Person();
            pers.setId(event.getInt("id"));
            pers.setLastName(event.getString("lastName"));
            pers.setFirstName(event.getString("firstName"));
            pers.setAge(event.getInt("age"));
            if ((event.getInt("age") > 0) && (event.getInt("age") < 100)) {
                persons.add(pers);
            } else {
                nextUrl = getNextURL("ERROR");
            }
            SerializationToXml.serializeObjectToXml(persons);
        } catch (NumberFormatException exception) {
            nextUrl = getNextURL("ERROR");
        }
        return nextUrl;
    }
}